(function() {
    'use strict';

    angular
        .module('zhserverApp')
        .controller('ChuFangMySuffixDialogController', ChuFangMySuffixDialogController);

    ChuFangMySuffixDialogController.$inject = ['$resource','BingMing','$timeout', '$scope', '$stateParams', '$uibModalInstance', '$q', 'entity', 'ChuFang', 'ZhengXing'];

    function ChuFangMySuffixDialogController ($resource,BingMing,$timeout, $scope, $stateParams, $uibModalInstance, $q, entity, ChuFang, ZhengXing) {
        var vm = this;
        vm.bName=null;
        vm.chuFang = entity;
        console.log(vm.chuFang);
        if(vm.chuFang.id!=null){
            vm.bName=vm.chuFang.zhengxing.bingming.bmName;
        }

        vm.clear = clear;
        vm.save = save;
        //病名
        vm.bNames=null;



        var urlBM= "api/bing-mings/All";
        var zxm_resource=$resource(urlBM);
        zxm_resource.query(function (data) {
            vm.bNames=data;
        }, function (error) {
            console.log(error.data.message);
        });


        $timeout(function (){
            angular.element('.form-group:eq(1)>input').focus();
        });

        function clear () {
            $uibModalInstance.dismiss('cancel');
        }

        function save () {
            vm.isSaving = true;
            if (vm.chuFang.id !== null) {

                ChuFang.update(vm.chuFang, onSaveSuccess, onSaveError);
            } else {
                ChuFang.save(vm.chuFang, onSaveSuccess, onSaveError);
            }
        }

        function onSaveSuccess (result) {
            $scope.$emit('zhserverApp:chuFangUpdate', result);
            $uibModalInstance.close(result);
            vm.isSaving = false;
        }

        function onSaveError () {
            vm.isSaving = false;
        }

        $scope.$watch('vm.bName',function(newValue,oldValue, scope){

            if (newValue!=null) {
                //获取证型
                var url2= "api/zheng-xings-byBM/"+newValue;
                var zxm_resource=$resource(url2);
                zxm_resource.query(function (data) {
                    vm.zhengxings=data;
                }, function (error) {
                    console.log(error.data.message);
                });
            }else{
                vm.zhengxings=null;
            }
        });


    }
})();
